#!/bin/sh
#
#

DTB_DIR="/usr/local/share/rpi-firmware"
DTB="bcm2710-rpi-2-b.dtb bcm2710-rpi-3-b.dtb bcm2710-rpi-3-b-plus.dtb \
	bcm2710-rpi-cm3.dtb bcm2710-rpi-zero-2-w.dtb bcm2711-rpi-4-b.dtb \
	bcm2711-rpi-cm4-io.dtb bcm2711-rpi-cm4.dtb"
EMBEDDED_TARGET_ARCH="aarch64"
EMBEDDED_TARGET="arm64"
EMBEDDEDBUILD=1
EMBEDDEDPORTS="sysutils/u-boot-rpi-arm64 sysutils/rpi-firmware"
FAT_SIZE="50m -b 1m"
FAT_TYPE="16"
IMAGE_SIZE="5120M"
KERNEL="GENERIC"
MD_ARGS="-x 63 -y 255"
OL_DIR="${DTB_DIR}/overlays"
OVERLAYS="mmc.dtbo pwm.dtbo disable-bt.dtbo"
PART_SCHEME="MBR"
export BOARDNAME="RPI"
CONFIG_POWERD_ENABLE=1

arm_install_uboot() {
	UBOOT_DIR="/usr/local/share/u-boot/u-boot-rpi-arm64"
	UBOOT_FILES="README u-boot.bin"
	DTB_FILES="armstub8.bin armstub8-gic.bin bootcode.bin fixup_cd.dat \
		fixup_db.dat fixup_x.dat fixup.dat LICENCE.broadcom \
		start_cd.elf start_db.elf start_x.elf start.elf \
		fixup4.dat fixup4cd.dat fixup4db.dat fixup4x.dat start4.elf \
		start4cd.elf start4db.elf start4x.elf ${DTB}"
	FATMOUNT="${DESTDIR%${KERNEL}}fat"
	chroot ${CHROOTDIR} mkdir -p "${FATMOUNT}"
	chroot ${CHROOTDIR} mount_msdosfs /dev/${mddev}s1 ${FATMOUNT}
	for _UF in ${UBOOT_FILES}; do
		chroot ${CHROOTDIR} cp -p ${UBOOT_DIR}/${_UF} \
			${FATMOUNT}/${_UF}
	done
	for _DF in ${DTB_FILES}; do
		chroot ${CHROOTDIR} cp -p ${DTB_DIR}/${_DF} \
			${FATMOUNT}/${_DF}
	done
	chroot ${CHROOTDIR} cp -p ${DTB_DIR}/config_arm64.txt \
			${FATMOUNT}/config.txt
	chroot ${CHROOTDIR} mkdir -p ${FATMOUNT}/overlays
	for _OL in ${OVERLAYS}; do
		chroot ${CHROOTDIR} cp -p ${OL_DIR}/${_OL} \
			${FATMOUNT}/overlays/${_OL}
	done

	sync
	umount_loop ${CHROOTDIR}/${FATMOUNT}
	chroot ${CHROOTDIR} rmdir ${FATMOUNT}
	
	return 0
}
